package com.rlz.base.util.single

/**
 * Created by RLZ
 * on 2021/10/15
 *
 * 快速创建一个 无参数的 单例类
 */
open class Single<out T> constructor(creator: () -> T) {

    private var mCreator: (() -> T)? = creator

    @Volatile
    private var mInstance: T? = null

    fun get(): T {
        val instance = mInstance
        if (instance != null)
            return instance

        return synchronized(this) {
            val ins = mInstance
            if (ins != null) ins
            else {
                val created = mCreator!!()
                mInstance = created
                mCreator = null
                created
            }
        }
    }
}